const Base = require('./base.js');

module.exports = class extends Base {
  async getAction() {
    const id = this.get('id');
    if (id) {
      const data = await this.model('module').where({id, deleted_at: null}).find();
      return this.success(data);
    } else {
      const {page = 1, pagesize = 10} = this.get();
      const list = await this.model('module').where({deleted_at: null}).order('sort ASC').page(page, pagesize).countSelect();
      return this.success(list);
    }
  }

  async addAction() {
    const {name, descript, status, sort} = this.post();
    try {
      const res = await this.model('module').add({name, descript, status, sort});
      this.success(res);
    } catch (e) {
      this.fail(e.message);
    }
  }
  async changeAction() {
    const {name, descript, status, sort, id} = this.post();
    try {
      const res = await this.model('module').where({id}).update({name, descript, status, sort});
      this.success(res);
    } catch (e) {
      this.fail(e.message);
    }
  }

  async delAction() {
    const {id} = this.post();
    try {
      const res = await this.model('module').where({id}).delete();
      this.success(res);
    } catch (e) {
      this.fail(e.message);
    }
  }
};
